package vn.student.controller;

import java.util.ArrayList;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.ViewAssignmentDetail;
import vn.student.ViewClassDetail;
import vn.student.mylib.Assignment;
import vn.student.mylib.Class;
import vn.student.mylib.Constants;
import vn.student.mylib.ListAssignmentInPlannerAdapter;
import vn.student.mylib.ListClassAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class ViewDateScheduleController {
	AStudentDB myDB = null;
	Activity act = null;
	String dateStr = "";
	public ViewDateScheduleController(Activity act, String dateStr){
		this.act = act;
		myDB = new AStudentDB(act);
		this.dateStr = dateStr;
	}
	public void showDateClass() {
		ArrayList<Class> listClass = myDB.getClassListInDate(dateStr); 
		if(listClass==null||listClass.size()==0)
			return;
		ListView listClassView = (ListView)act.findViewById(R.id.listView);
		listClassView.getLayoutParams().height=107*listClass.size()+10;
		listClassView.setAdapter(new ListClassAdapter(act,listClass,false));
		listClassView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(act,ViewClassDetail.class);
				Bundle bundle = new Bundle();
				ListClassAdapter adapter = (ListClassAdapter) arg0.getAdapter();
				ArrayList<Class> list = adapter.list;
				bundle.putString("id", list.get(arg2).id);
				bundle.putBoolean("isViewStudentAss", true);
				intent.putExtras(bundle);
				act.startActivityForResult(intent, Constants.ViewClassDetailRequestCode);
			}
		});
	}
	public void showDateAssignments() {
		ArrayList<Assignment> listAssignment = myDB.getAssignmentInDateList(dateStr);
		if(listAssignment==null)
			return;
		ListView listAssignmentView = (ListView) act.findViewById(R.id.listView);
		listAssignmentView.setAdapter(new ListAssignmentInPlannerAdapter(act,listAssignment,true));
		listAssignmentView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(act,ViewAssignmentDetail.class);
				Bundle bundle = new Bundle();
				ListAssignmentInPlannerAdapter adapter = (ListAssignmentInPlannerAdapter) arg0.getAdapter();
				ArrayList<Assignment> list = adapter.list;
				bundle.putString("id", list.get(arg2).id);
				bundle.putBoolean("isEdit", false);
				bundle.putBoolean("isCreateNew", false);
				bundle.putString("classId", "-1");
				intent.putExtras(bundle);
				act.startActivityForResult(intent, Constants.ViewAssignmentDetailInPlannerRequestCode);
			}
		});
	}
}
